package cn.wolfcode.luowowo.controller;


import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParamLogin;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;


    @Autowired
    private IStrategyCommentService strategyCommentService;


    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    /**
     * redis 缓存阅读数
     */
    @Autowired
    private IStrategyStatisService strategyStatisService;


    /**
     * // 根据攻略明细Id查询攻略明细
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Object detail(String id){
        /**
         * 根据传上来的明细Id查找明细对象
         */
        Strategy strategy = strategyService.get(id);

        /**
         * 查看某篇攻略同时把阅读量 +  1
         */
        strategyStatisService.incrementviewnum(id);

        return JsonResult.success(strategy);

    }


    /**
     * 查询全部攻略明细
     * @param qo
     * @return
     */
    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        Page<Strategy> query = strategyService.query(qo);
        return JsonResult.success(query);
    }


    /**
     * 全部攻略主题
     * @param qo
     * @return
     */
    @GetMapping("/themes")
    public Object themes(StrategyQuery qo){
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }


    /**
     * 新增攻略评论
     * @param strategyComment
     * @return
     */
    @PostMapping("/addComment")
    @RequireLogin
    public Object addComment(StrategyComment strategyComment, @UserParamLogin UserInfo userInfo){
//        // 通过请求头拿到当前登陆对象
//        String token = request.getHeader("token");
        if (userInfo != null) {
//            UserInfo userInfo = userInfoRedisService.getToken(token);
            // Bean工具类  会把两个对象相同的名字的属性转移过去
            BeanUtils.copyProperties(userInfo,strategyComment);
            // 名字不同要自己转换
            strategyComment.setUserId(userInfo.getId());
        }
        // 时间设置
        strategyComment.setCreateTime(new Date());
        // 执行插入方法
        strategyCommentService.save(strategyComment);

        /**
         * 评论攻略后 评论数 + 1
         */
        strategyStatisService.incrementreplynum(strategyComment.getStrategyId());

        return JsonResult.success();
    }


    /**
     * 根据对应攻略的Id去查找全部攻略评论
     * @return
     */
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        Page<StrategyComment> query = strategyCommentService.query(qo);
        return JsonResult.success(query);
    }

    /**
     * 点赞的
     * @param cid
     * @return
     */
    @PostMapping("/commentThumb")
    @RequireLogin
    private Object commentThumb(String cid ,@UserParamLogin UserInfo userInfo){
//        String token = request.getHeader("token");
        if (userInfo != null){
//            UserInfo userInfo = userInfoRedisService.getToken(token);
            strategyCommentService.commentThumb(cid,userInfo.getId());
        }
        return JsonResult.success();
    }


    /**
     * 拿到当前登陆对象
     * @param userInfo
     * @return
     */
    @GetMapping("/info")
    public Object info(@UserParamLogin UserInfo userInfo){
        return JsonResult.success(userInfo);
    }



    /**
     * 拿到Vo对象返回回去
     * @param sid
     * @return
     */
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        StrategyStatisVO vo = strategyStatisService.getStrategyStatisVO(sid);
        return JsonResult.success(vo);
    }


    /**
     * 第一次收藏和第二次取消收藏
     * @param sid
     * @param userInfo
     * @return
     */
    @PostMapping("/favor")
    @RequireLogin
    public Object favor(String sid,@UserParamLogin UserInfo userInfo){
        boolean ret = strategyStatisService.favor(sid,userInfo.getId());
        return JsonResult.success(ret);
    }


    @PostMapping("/strategyThumbup")
    @RequireLogin
    public Object strategyThumbup(String sid, @UserParamLogin UserInfo userInfo){

      boolean ret = strategyStatisService.strategyThumbup(sid,userInfo.getId());


        return JsonResult.success(ret);
    }


    /**
     * 分享
     * @param sid
     * @param userInfo
     * @return
     */
    @GetMapping("/strategyShare")
    @RequireLogin
    public Object strategyShare(String sid,@UserParamLogin UserInfo userInfo){
        boolean ret = strategyStatisService.strategyShare(sid,userInfo.getId());
        return JsonResult.success(ret);
    }

}
